package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IFlowerCountDao;
import com.hamizhong.iflowers.db.entity.FlowerCount;
import com.hamizhong.iflowers.service.IFlowerCountService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.FlowerCountVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class FlowerCountServiceImpl extends MyBaseService implements IFlowerCountService {
	private IFlowerCountDao iFlowerCountDao = null;

	public IFlowerCountDao getiFlowerCountDao() {
		return iFlowerCountDao;
	}
	public void setiFlowerCountDao(IFlowerCountDao iFlowerCountDao) {
		this.iFlowerCountDao = iFlowerCountDao;
	}
	
	public IOPResultDto<IObjectVO> findAll(Integer pageNum, int pageSize) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<FlowerCount> flowerCountList = new ArrayList<FlowerCount>();
		flowerCountList = iFlowerCountDao.findFlowerCount(pageNum, pageSize);
		int totalFlowerCountCnt = iFlowerCountDao.findTotalFlowerCountCnt();
		List<FlowerCountVO> flowerCountVOList = new ArrayList<FlowerCountVO>();
		FlowerCountVO flowerCountVO = null;
		for (FlowerCount flowerCount : flowerCountList) {
			flowerCountVO = new FlowerCountVO();
			BeanUtils.copyProperties(flowerCount, flowerCountVO);
			flowerCountVOList.add(flowerCountVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setFlowerCountVOList(flowerCountVOList);
		iObjectVO.setTotalFlowerCountCnt(totalFlowerCountCnt);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	public IOPResultDto<IObjectVO> addFlowerCount(FlowerCountVO flowerCountVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		FlowerCount flowerCount = new FlowerCount();
		try {
			BeanUtils.copyProperties(flowerCountVO, flowerCount);
			iFlowerCountDao.add(flowerCount);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> updateFlowerCount(FlowerCountVO flowerCountVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		FlowerCount flowerCount = new FlowerCount();
		try {
			BeanUtils.copyProperties(flowerCountVO, flowerCount);
			iFlowerCountDao.update(flowerCount);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> deleteFlowerCount(FlowerCountVO flowerCountVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			int flowerCountId = flowerCountVO.getFlowerCountId();
			FlowerCount flowerCount = iFlowerCountDao.findById(flowerCountId);
			if(null != flowerCount) {
				this.iFlowerCountDao.delete(flowerCount);
				dto.setSuccess(true);
			}else {
				dto.setSuccess(false);
				dto.setErrorMSG(MyErrorMsgConstants.NOT_EXIST_DATA_FOR_OPERATE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.ERR_SYSTEM);
			dto.setSuccess(false);
		}
		return dto;
	}
	
	
}
